package cn.kgc.servlet;

import cn.kgc.domain.User;
import cn.kgc.service.UserService;
import cn.kgc.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author Rock
 * @create 2019-11-25 16:58
 */
@WebServlet("/findUserConditionServlet")
public class FindUserConditionServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        UserService service = new UserServiceImpl();
        try {
            BeanUtils.populate(user,map);
            List<User> users = service.findUserByCondition(user);
            req.setAttribute("users",users);
            req.getRequestDispatcher("list.jsp").forward(req,resp);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
